/**
 * 办公系统
 * 
 * @author 孙晓晔
 * @version $Id: common.js 1031 2010-03-24 11:32:00Z sunxy $
 */

var tab;

$(function() {
	$("#abc").addClass("ui-state-default ui-corner-all");
	$("#abc").css({ "padding":" .2em", "text-decoration": "none"});

	$("#abc").floatdiv({top:"25px",left:"5px"});	
	
	tabs = $('#tabs').tabs({
		// tabTemplate: '<li><a href="#{href}">#{label}</a> <span class="ui-icon ui-icon-close">Remove Tab</span></li>',
		add: function(event, ui) {
			$(ui.tab).parents('li:first')
	        .append('<span class="ui-tabs-close ui-icon ui-icon-close" title="关闭"></span>')
	        .find('span.ui-tabs-close')
	        .click(function() {
	        	tabs.tabs('remove', $('li', tabs).index($(this).parents('li:first')[0]));
	        });
	    
			tabs.tabs('select', '#' + ui.panel.id);
		}
	});
	
	var flip = 1;
	$("#abc").click(function() {
		flip++;
		$("#CategoryTree").toggle();
		if (flip % 2 == 0) {
			$("#ddd").width(0);
			$("#abc").html("&raquo;");
		} else {
			$("#ddd").width(200);
			$("#abc").html("&laquo;");
		}		

		return false;
	});
	
	$("#dialog_login").dialog( {
		bgiframe : true,
		width : 360,
		height : 165,
		resizable : false,
		draggable : false,
		autoOpen : false
	});
	
	$('#dialog_login').dialog('option', 'title', '景荣办公系统');
	
	var _dialog_option = {
		modal : true,
		autoOpen : false,
		resizeStop : function(event, ui) {
			var width = $(this).dialog('option', 'width');
			var height = $(this).dialog('option', 'height');
			$('#dialog').html(width + "x" + height);
		},

		beforeClose : function(event, ui) {
			$(this).text('');
			$(this).dialog('option', 'height', 'auto');
		},
		
		close : function(event, ui) {
			$(this).dialog('option', 'buttons', {});
		}
	};

	$("#dialog_add").dialog(_dialog_option);
	$("#dialog_edit").dialog(_dialog_option);
	$("#dialog_view").dialog(_dialog_option);
	$("#dialog_emp").dialog(_dialog_option);
	$("#dialog_qr").dialog(_dialog_option);
	
	$("#placetab").hide();
});

/*
$().ajaxSend(function() {
	$.blockUI({ message: $('#loading') });
});

$().ajaxStart(function() {
	$.blockUI({ message: $('#loading') });
});
*/

function menutree(o) {
	if (o.parentNode) {
		if (o.parentNode.className == "Opened") {
			o.parentNode.className = "Closed";
		} else {
			o.parentNode.className = "Opened";
		}
	}
}
/*
$(document).ajaxStart(function() {	
	$.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});
}).ajaxStop($.unblockUI);
*/

$(document).ajaxStop($.unblockUI);

function menu(sURL, sView) {
	$("#placetab").hide();
	$(sView).show();
	
	// $.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});

	sURL = _url(sURL);

	$.get(sURL, function(data) {/*
		if (data != null && data == '1') {
			$('#dialog_login').dialog('open');
		} else {*/
			$(sView).html(data);
		// }
	});
}

function tab(sURL, sView, tab, title) {
	$("#placeholder").html('');
	// $("#placeholder").hide();
	$(sView).show();
			
	// $.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});
	
	$.get(sURL, function(data) {
		if (data != null && data == '1') {
			$('#dialog_login').dialog('open');
		} else {
			var d = 10000 * Math.random();
			// sURL = sPath + "?" + aParams.join("&") + "&rnd=" + Math.ceil(d);
			tab = Math.ceil(d);
			
			var t = 'tabs'+tab;	
			// alert(t);
			tabs.tabs('add', "#"+t, title);
			$("#"+t, "#tabs").load(sURL);
			tabs.tabs('select', "#"+t);
		}
	});
}

function tab22(sURL, sView, tab, title) {
	$("#placeholder").html('');
	// $("#placeholder").hide();
	$(sView).show();
	
	// $.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});
			
	$.get(sURL, function(data) {
		if (data != null && data == '1') {
			$('#dialog_login').dialog('open');
		} else {		
			var t = '#tabs'+tab;
			// alert(t);
			if($(t).html() == null ) {
				tabs.tabs('add', t, title);
				$(t, "#tabs").load(sURL);
				tabs.tabs('select', t);
			} else {				
				tabs.tabs('select', t);
			}
		}
	});
}

function dialog_import(url, title) {
	dialog_add(url, title, '600x240');
}

function dialog_add(url, title, xy) {
	dialog_x(url, title, xy, '#dialog_add');
}

function dialog_edit(url, title, xy) {
	dialog_x(url, title, xy, '#dialog_edit');
}

function dialog_view(url, title, xy) {
	dialog_x(url, title, xy, '#dialog_view');
}

function dialog_x(url, title, xy, view) {
	$.get(url, function(data) {
		$(view).html(data);
	});

	var regex = /^(\d+)(x)(\d+)$/;
	if (regex.test(xy)) {
		var cc = xy.split("x");
		var width = parseInt(cc[0]);
		var height = parseInt(cc[1]);
		$(view).dialog('option', 'height', height);
	} else {
		var width = parseInt(xy);
	}

	$(view).dialog('option', 'title', title);
	$(view).dialog('option', 'width', width);
	$(view).dialog('open');
}

function dialog_t(data, title, xy, view) {
	
	$(view).html(data);

	var regex = /^(\d+)(x)(\d+)$/;
	if (regex.test(xy)) {
		var cc = xy.split("x");
		var width = parseInt(cc[0]);
		var height = parseInt(cc[1]);
		$(view).dialog('option', 'height', height);
	} else {
		var width = parseInt(xy);
	}

	$(view).dialog('option', 'title', title);
	$(view).dialog('option', 'width', width);
	$(view).dialog('open');
}

function _error() {	
	$('#dialog_view').dialog('option', 'buttons', {
		"关闭": function() { 
			$(this).dialog("close");
		}		
	});

	var data = "权限受限，请联系管理员！";	

	dialog_view_info(data, '提示信息', '320x160');
}

function dialog_view_info(data, title, xy) {
	if (data == null) {
		data = '权限受限，请联系管理员！';
	}

	if (title == null) {
		title = '提示信息';
	}

	if (xy == null) {
		xy = '320x160';
	}

	$('#dialog_view').html(data);

	var regex = /^(\d+)(x)(\d+)$/;
	if (regex.test(xy)) {
		var cc = xy.split("x");
		var width = parseInt(cc[0]);
		var height = parseInt(cc[1]);
		$('#dialog_view').dialog('option', 'height', height);
	} else {
		var width = parseInt(xy);
	}

	$('#dialog_view').dialog('option', 'title', title);
	$('#dialog_view').dialog('option', 'width', width);
	$('#dialog_view').dialog('open');
}

function view(url, title, xy) {	
	_dialog_view(url, title, xy);
}

function _dialog_view(url, title, xy) {
	$('#dialog_view').dialog('option', 'buttons', { 		
		"关闭": function() { 
			$(this).dialog("close");
		}
	});

	dialog_view(url, title, xy);
}

function emp(id) {
	var url = "emp.php?do=Emp.view&id=" + id;
	$('#dialog_emp').dialog('option', 'buttons', { 		
		"关闭": function() { 
			$(this).dialog("close");
		}
	});

	dialog_x(url, '员工信息', '500x320', '#dialog_emp');
}

function qr(url) {
	// var url = "emp.php?do=Emp.view&id=" + id;
	$('#dialog_qr').dialog('option', 'buttons', { 		
		"关闭": function() { 
			$(this).dialog("close");
		}
	});

	dialog_x(url, '二维码', '600x400', '#dialog_qr');
}

function mold(id) {
	var url = "mold.php?do=Mold.view&id=" + id;
	
	_dialog_view(url, '产品信息', '780x450');
}

function list(url, view) {
	// $.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});
	
	if (view == null || view == undefined) {
		var s = $("div[id^='result']:visible");
		view = s[0].id;
	}
	
	$.get(url, function(data) {
		$('#'+view).html(data);
	});
}

function post_find(form, result) {
	if (result == null || result == 'undefined') {
		var s = $("div[id^='result']:visible");
				
		result = s[0].id;
	}
	
	// $.blockUI({ message: $('#loading'), css: {padding: 5, margin: 5}});
	
	if (typeof form == 'undefined') {
		var form = document.forms[0];
	}

	var url = _url(form.action);
	// var body = _body(form);
	var body = $(form).formSerialize();
	// alert(body);

	$.post(url, body, function(data) {
		if (data == '1') {
			$('#dialog_login').dialog('open');
		} else {
			
			$('#'+result).html(data);
		}
	});
	
	return false;
}

function find(form, result) {
	post_find(form, result);
}

function _url(sURL) {
	var sPath = sURL.slice(0, sURL.indexOf("?"));
	var sQuery = sURL.slice(sURL.indexOf("?") + 1, sURL.length);

	var aPairs = sQuery.split("&");
	var aParams = new Array();
	for ( var i = 0; i < aPairs.length; i++) {
		var aPair = aPairs[i].split("=");

		var sParam = encodeURIComponent(aPair[0]);
		sParam += "=";
		// sParam += encodeURIComponent(aPair[1]);
		sParam += aPair[1];

		aParams.push(sParam);
	}

	var d = 10000 * Math.random();
	sURL = sPath + "?" + aParams.join("&") + "&rnd=" + Math.ceil(d);

	return sURL;
}

function _body(form) {
	var aParams = new Array();
	var len = form.elements.length;
	for ( var i = 0; i < len; i++) {
		var type = form.elements[i].type;
		if (type == 'button') {
			continue;
		}

		if (type == 'submit') {
			continue;
		}

		if (type == 'reset') {
			continue;
		}

		if (type == 'checkbox'
				&& form.elements[i].checked == false) {
			continue;
		}

		if (type == 'radio'
				&& form.elements[i].checked == false) {
			continue;
		}
		
		var val = form.elements[i].value;
		if (val != null && val.length > 0) {
			var sParam = encodeURIComponent(form.elements[i].name);
			sParam += "=";
			sParam += encodeURIComponent(val.trim());

			aParams.push(sParam);
		}
	}

	return aParams.join("&");
}

function _param(oForm) {
	var aParams = new Array();
	var sParam;

	for ( var i = 0; i < oForm.elements.length; i++) {
		sParam = oForm.elements[i];
		if (sParam.name.substring(0, 2) == 'c_' && sParam.checked) {
			aParams.push(oForm.elements[i].value);
		}
	}

	return aParams.join(",");
}

function jswin(url, winid, xy) {
	var cc = xy.split("x");
	var width = cc[0];
	var height = cc[1];

	openwin(url, winid, width, height);
}

function openwin(url, winid, w, h) {
	var _left = screen.width / 2 - w / 2;
	var _top = screen.height / 2 - h / 2;

	var newwin = window
			.open(
					url,
					winid,
					"toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,fullscreen=no,copyhistory=no,top="
							+ _top
							+ ",left="
							+ _left
							+ ", width="
							+ w
							+ ",height=" + h);
	newwin.focus();
	return false;
}

function chkbox(form) {
	var len = form.elements.length;
	for ( var i = 0; i < len; i++) {
		var e = form.elements[i];
		if (e.name != 'chkall') {
			e.checked = form.chkall.checked;
		}
	}
}

function expunge(form, result) {
	var count = 0;
	var len = form.elements.length;
	for ( var i = 0; i < len; i++) {
		var e = form.elements[i];
		var name = e.name.substring(0, 2);
		if (name == 'c_' && e.checked) {
			count++;
		}
	}

	if (confirm("您确定删除已选择的" + count + "条记录吗？")) {
		find(form, result);
		return true;
	}

	return false;
}

function fmoney(s, n) {
   n = n > 0 && n <= 20 ? n : 2;
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
   var l = s.split(".")[0].split("").reverse();
   var r = s.split(".")[1];
   var t = "";
   for (i = 0; i < l.length; i ++ ) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
   }

   return t.split("").reverse().join("") + "." + r;
}

function query(sQuery) {
	var aPairs = sQuery.split("&");
	var aParams = new Array();
	for ( var i = 0; i < aPairs.length; i++) {
		var aPair = aPairs[i].split("=");
		if (/(_s|_s\[\])$/.test(aPair[0]) || aPair[0] == 'p') {
			var sParam = encodeURIComponent(aPair[0]);
			sParam += "=";
			sParam += encodeURIComponent(aPair[1]);

			aParams.push(sParam);
		}		
	}

	return "&" + aParams.join("&");
}


function fxmul(a, b) {
	return fxmultiply(a, b);
}

function fxmultiply(a, b) {
	if (is_null(a) || is_null(b)) {
		return '';
	}
	
	var f1 = String(a).split(".").length > 1 ? String(a).split(".")[1].length : 0;
	var f2 = String(b).split(".").length > 1 ? String(b).split(".")[1].length : 0;
	var aa = String(a).replace(/^0*|\./g, "");
	var bb = String(b).replace(/^0*|\./g, "");   
	
	return Number(aa) * Number(bb) / Math.pow(10, f1 + f2);
}

function fxadd(a, b) {
	if (is_null(a) && is_null(b)) {
		return 0;
	}
	
	if (is_null(a)) {
		return b;
	}
	
	if (is_null(b)) {
		return a;
	}
	
	var f1 = String(a).split(".").length > 1 ? String(a).split(".")[1].length : 0;
	var f2 = String(b).split(".").length > 1 ? String(b).split(".")[1].length : 0;
	var m = Math.pow(10, Math.max(f1, f2));
	
	return (a*m + b*m) / m;
}

function is_null(s) {
	if (s == null || s == 'undefined') {
		return true;
	}
	
	if (s == '') {
		return true;
	}
	
	return false;
}

function current_date() {
	var d = new Date();
	var y = d.getFullYear();
	var m = d.getMonth() + 1;
	var d = d.getDate();
	
	var mm = m < 10 ? "0" + m : m;
	var dd = d < 10 ? "0" + d : d;
	
	var ret = y + "-" + mm + "-" + dd;
	// alert(ret);
	
	return ret;
}

function _change(id, id_t) {
	$(id_t).change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			$(id).val(s);
		}		
	});
}

function _ck_default(id, id_t) {
	$(id_t).click(function() {
		var s = $(id);
		if ($(this).attr("checked")) {
			s.val($(this).val());
		} else {
			s.val("");
		}
	});
}

/*
 * 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时：分钟：秒
 * 其中，年月日为全格式，例如 ： 2010-10-12 01:00:00
 * 返回精度为：秒，分，小时，天
 */
function GetDateDiff(startTime, endTime, diffType) {
	//将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
	startTime = startTime.replace(/\-/g, "/");
	endTime = endTime.replace(/\-/g, "/");

	//将计算间隔类性字符转换为小写
	diffType = diffType.toLowerCase();
	var sTime = new Date(startTime);      //开始时间
	var eTime = new Date(endTime);  //结束时间
	//作为除数的数字
	var divNum = 1;
	switch (diffType) {
	case "second":
		divNum = 1000;
		break;
	case "minute":
		divNum = 1000 * 60;
		break;
	case "hour":
		divNum = 1000 * 3600;
		break;
	case "day":
		divNum = 1000 * 3600 * 24;
		break;
	default:
		break;
	}
	
	return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}

function next_month(d1) {
	var time = Date.parse(d1);
	var date = new Date();
	date.setTime(time);
	
	date.setMonth(date.getMonth() + 1);
			
	return $.dateFormat.date(date, "yyyy-MM-dd");
}
